package org.gdstash.ui.util;

import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.GDTabbedSearchDialog;
import org.gdstash.ui.select.ItemFullSelectionPane;
import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CollapsiblePanel extends AdjustablePanel {
    private JButton btnCollapse;
    private boolean collapsed;
    private String tagTitle;
    private AdjustablePanel panel;
    private AdjustableTabbedPane tabbedPane;
    private int direction;

    public CollapsiblePanel(final AdjustablePanel panel, final String tagTitle) {
        this(panel, tagTitle, 0, false);
    }

    public CollapsiblePanel(final AdjustablePanel panel, final String tagTitle, final int direction) {
        this(panel, tagTitle, direction, false);
    }

    public CollapsiblePanel(final AdjustablePanel panel, final String tagTitle, final boolean collapsed) {
        this(panel, tagTitle, 0, collapsed);
    }

    public CollapsiblePanel(final AdjustablePanel panel, final String tagTitle, final int direction, final boolean collapsed) {
        this.panel = panel;
        this.tabbedPane = null;
        this.tagTitle = tagTitle;
        this.collapsed = collapsed;
        this.direction = direction;
        this.adjustUI();
        final BorderLayout layout = new BorderLayout();
        this.setLayout(layout);
        if (direction == 0) {
            this.add(this.btnCollapse, "North");
        } else {
            this.add(this.btnCollapse, "West");
        }
        this.add(panel, "Center");
        if (collapsed) {
            panel.setVisible(false);
        } else {
            panel.setVisible(true);
        }
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        this.setBorder(compound);
    }

    public CollapsiblePanel(final AdjustableTabbedPane tabbedPane, final String tagTitle) {
        this(tabbedPane, tagTitle, 0, false);
    }

    public CollapsiblePanel(final AdjustableTabbedPane tabbedPane, final String tagTitle, final int direction) {
        this(tabbedPane, tagTitle, direction, false);
    }

    public CollapsiblePanel(final AdjustableTabbedPane tabbedPane, final String tagTitle, final boolean collapsed) {
        this(tabbedPane, tagTitle, 0, collapsed);
    }

    public CollapsiblePanel(final AdjustableTabbedPane tabbedPane, final String tagTitle, final int direction, final boolean collapsed) {
        this.panel = null;
        this.tabbedPane = tabbedPane;
        this.tagTitle = tagTitle;
        this.collapsed = collapsed;
        this.direction = direction;
        this.adjustUI();
        final BorderLayout layout = new BorderLayout();
        this.setLayout(layout);
        if (direction == 0) {
            this.add(this.btnCollapse, "North");
        } else {
            this.add(this.btnCollapse, "West");
        }
        this.add(tabbedPane, "Center");
        if (collapsed) {
            tabbedPane.setVisible(false);
        } else {
            tabbedPane.setVisible(true);
        }
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        this.setBorder(compound);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntButton = fntButton.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.btnCollapse == null) {
            (this.btnCollapse = new JButton()).addActionListener(new CollapsibleListener());
        }
        this.btnCollapse.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, this.tagTitle));
        if (this.collapsed) {
            this.btnCollapse.setIcon(GDImagePool.iconWindowExpand24);
        } else {
            this.btnCollapse.setIcon(GDImagePool.iconWindowCollapse24);
        }
        this.btnCollapse.setFont(fntButton);
        if (this.direction == 1) {
            this.btnCollapse.setUI(new VerticalButtonUI(270));
        }
        if (this.panel != null) {
            this.panel.adjustUI();
        }
        if (this.tabbedPane != null) {
            this.tabbedPane.adjustUI();
        }
    }

    public static void test() {
        final ItemFullSelectionPane pnlSel1 = new ItemFullSelectionPane(GDTabbedSearchDialog.Mode.SEARCH);
        final ItemFullSelectionPane pnlSel2 = new ItemFullSelectionPane(GDTabbedSearchDialog.Mode.SEARCH);
        final CollapsiblePanel pnl1 = new CollapsiblePanel(pnlSel1, "TXT_LANG_COUNTRY", 0);
        final CollapsiblePanel pnl2 = new CollapsiblePanel(pnlSel2, "TXT_LANG_COUNTRY", 0, true);
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnl1)).addGroup(layout.createParallelGroup().addComponent(pnl2));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnl1).addComponent(pnl2));
        layout.setVerticalGroup(vGroup);
        final JFrame frame = new JFrame();
        frame.add(panel);
        frame.setLocation(GDStashFrame.iniConfig.sectWindow.x, GDStashFrame.iniConfig.sectWindow.y);
        if (GDStashFrame.iniConfig.sectWindow.maximized) {
            frame.setExtendedState(frame.getExtendedState() | 0x6);
        } else {
            frame.setSize(GDStashFrame.iniConfig.sectWindow.w, GDStashFrame.iniConfig.sectWindow.h);
        }
        frame.setDefaultCloseOperation(3);
        frame.setVisible(true);
    }

    private class CollapsibleListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            CollapsiblePanel.this.collapsed = !CollapsiblePanel.this.collapsed;
            if (CollapsiblePanel.this.collapsed) {
                if (CollapsiblePanel.this.panel != null) {
                    CollapsiblePanel.this.panel.setVisible(false);
                }
                if (CollapsiblePanel.this.tabbedPane != null) {
                    CollapsiblePanel.this.tabbedPane.setVisible(false);
                }
                CollapsiblePanel.this.btnCollapse.setIcon(GDImagePool.iconWindowExpand24);
            } else {
                if (CollapsiblePanel.this.panel != null) {
                    CollapsiblePanel.this.panel.setVisible(true);
                }
                if (CollapsiblePanel.this.tabbedPane != null) {
                    CollapsiblePanel.this.tabbedPane.setVisible(true);
                }
                CollapsiblePanel.this.btnCollapse.setIcon(GDImagePool.iconWindowCollapse24);
            }
            CollapsiblePanel.this.validate();
            CollapsiblePanel.this.repaint();
        }
    }
}
